import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

public class kfkConsumer {
    public static void main(String[] args) {
/*   1)bootstrap server ,key,value的反序列化
     2)创建kafka消费者组，设置kafka的消费者id
     3）订阅kafka的topic
     4）通过循环的形式，打印
* */

//      配置的消费者 配置信息  服务地址,key value
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "123.56.187.176:9092");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "HaHa");
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
//构建kafka的消费者
        KafkaConsumer<String, String> kfkconsumer = new KafkaConsumer<String, String>(properties);


//        topic name test1
//        订阅topic name
        kfkconsumer.subscribe(Arrays.asList("15test"));
//        循环消费
        while (true) {
//            通过poll形式拉取
            ConsumerRecords<String, String> conRecord = kfkconsumer.poll(2);
//           循环遍历
            for (ConsumerRecord<String, String> consumer : conRecord) {
                System.out.println("value is" + consumer.value());
            }
        }
    }
}
